package cn.shyouchi.wuguanjia.android.ui.event.report;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PreviewReportActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_sel_post)
    TextView mTvSelPost;
    @BindView(R.id.tv_sel_post_value)
    TextView mTvSelPostValue;
    @BindView(R.id.iv_post_has_next)
    ImageView mIvPostHasNext;
    @BindView(R.id.item_sel_post)
    RelativeLayout mItemSelPost;
    @BindView(R.id.tv_report_time_dif)
    TextView mTvReportTimeDif;
    @BindView(R.id.tv_report_time_dif_value)
    TextView mTvReportTimeDifValue;
    @BindView(R.id.iv_dif_has_next)
    ImageView mIvDifHasNext;
    @BindView(R.id.item_report_time_dif)
    RelativeLayout mItemReportTimeDif;
    @BindView(R.id.tv_sel_order)
    TextView mTvSelOrder;
    @BindView(R.id.tv_sel_order_value)
    TextView mTvSelOrderValue;
    @BindView(R.id.iv_order_has_next)
    ImageView mIvOrderHasNext;
    @BindView(R.id.item_sel_order)
    RelativeLayout mItemSelOrder;
    @BindView(R.id.tv_report_time)
    TextView mTvReportTime;
    @BindView(R.id.item_report_time)
    RelativeLayout mItemReportTime;
    @BindView(R.id.rv_report_times)
    RecyclerView mRvReportTimes;
    @BindView(R.id.btn_del)
    Button mBtnDel;
    @BindView(R.id.btn_edit)
    Button mBtnEdit;

    ReportEntity.ResultMessageBean entity;

    ReportTimeAdapter mReportTimeAdapter;

    List<String> times = new ArrayList<>();

    String projectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView() {
        return R.layout.activity_preview_report;
    }

    @Override
    public void initInjector() {
        entity = getIntent().getParcelableExtra("report");
        projectId = getIntent().getStringExtra("projectId");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());
        mReportTimeAdapter = new ReportTimeAdapter(times, false);
        mTvSelOrderValue.setText(entity.getService().getServicesname());

        mRvReportTimes.setAdapter(mReportTimeAdapter);
        mRvReportTimes.setLayoutManager(new LinearLayoutManager(mContext));

        mTvSelPostValue.setText(entity.getPostsetting().getPost());
        mTvReportTimeDifValue.setText(entity.getJetlag());

        try {
            JSONArray ts = new JSONArray(entity.getReportingtime());
            for (int i=0;i<ts.length();i++) {
                times.add(ts.getString(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        mReportTimeAdapter.notifyDataSetChanged();


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @OnClick({R.id.btn_del, R.id.btn_edit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_del:
                EventApi eventApi = new EventApi(mContext);
                eventApi.delReport(String.valueOf(entity.getId()))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            EventBus.getDefault().post(new DataChangeEvent());
                            finish();
                        }
                    });
                break;
            case R.id.btn_edit:


                Intent intent = new Intent(mContext, AddEventReportActivity.class);
                intent.putExtra("report", entity);
                intent.putExtra("projectId", projectId);
                startActivity(intent);
                finish();

                break;
        }
    }
}
